/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClassFrame;

import GamePlay.CanvasGame;
import Object.Question;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Black Diamond
 */
public class Map {

    public static final int ROUND1 = 0;
    public static final int ROUND2 = 1;
    public static final int ROUND3 = 2;
    public static int levelMax = 0;
    public static int level = 0;
    public static int round = 0;
    public static int piece = 0;
    private Button[][] pArray = new Button[3][3];
    private Question[][] qArray = new Question[3][3];
    private Question specialQuestion;
    private Button[][] lArray = new Button[4][5];
    private Image sPieceImg;
    private CanvasGame canvas;

    public Map(CanvasGame canvas) {
        this.canvas = canvas;
    }

    public void initLevelMap() {
        int lv = 0;
        for (int i = 0; i < lArray.length; i++) {
            for (int j = 0; j < lArray[i].length; j++) {
                lv = j * lArray.length + i;
                if (lv <= levelMax) {
                    lArray[i][j] = new Button("", Resource.IMG_BTN_LEVEL, 10 + 56 * i, 22 + 58 * j);
                } else {
                    lArray[i][j] = new Button("", Resource.IMG_BTN_LEVEL_LOCK, 10 + 56 * i, 22 + 58 * j);
                }
            }
        }
    }

    public void drawLevelMap(Graphics g) {
        for (int i = 0; i < lArray.length; i++) {
            for (int j = 0; j < lArray[i].length; j++) {
                lArray[i][j].drawButton(g, canvas);
                if(j*4+i <= levelMax){
                    canvas.getImage().drawNumber(g, canvas, Resource.IMG_NUMBER2, j*4+i+1, lArray[i][j].getX() + lArray[i][j].getW()/2, lArray[i][j].getY()+13, ClassFrame.Image.CENTER);
                }
            }
        }
    }

    public void initRound(int round) {
        switch (round) {
            case ROUND1:
            case ROUND2:
//                pArray = new Button[3][3];
                pArray = new Button[3][3];
                qArray = new Question[3][3];
                for (int i = 0; i < pArray.length; i++) {
                    for (int j = 0; j < pArray[i].length; j++) {
                        qArray[i][j] = randQuestion();
                        qArray[i][j].setAnswered(false);
                        qArray[i][j].setRight(false);
                        pArray[i][j] = new Button(canvas, "VKL", Resource.IMG_BTN_PIECE, 31 + 60 * i, 86 + 60 * j, 60, 60);
                    }
                }
                specialQuestion = randQuestion();
                specialQuestion.setAnswered(false);
                specialQuestion.setRight(false);
                try {
                    System.out.println("Path: " + specialQuestion.getPath());
                    Image tmp = Image.createImage(specialQuestion.getPath());
                    sPieceImg = canvas.getImage().resizeImg(tmp, 180, 180);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                break;
            case ROUND3:
                pArray = new Button[5][5];
                qArray = new Question[5][5];
                for (int i = 0; i < pArray.length; i++) {
                    for (int j = 0; j < pArray[i].length; j++) {
                        qArray[i][j] = randQuestion();
                        qArray[i][j].setAnswered(false);
                        qArray[i][j].setRight(false);
                        pArray[i][j] = new Button(canvas, " ", Resource.IMG_BTN_PIECE_3, 31 + 36 * i, 86 + 36 * j, 36, 36);
                    }
                }
                break;
        }
    }

    public void drawPieceMap(Graphics g) {
        if (sPieceImg != null) {
            if(round == ROUND1){
                g.drawImage(sPieceImg, 31, 86, Graphics.TOP | Graphics.LEFT);
            }
        }
        for (int i = 0; i < pArray.length; i++) {
            for (int j = 0; j < pArray[i].length; j++) {
                if (qArray[i][j].isAnswered()) {
                    if (!qArray[i][j].isRight()) {
                        pArray[i][j].setState(Button.DISABLE);
                        pArray[i][j].drawButton(g);
                    }
                } else {
                    pArray[i][j].drawButton(g);
                }
            }
        }
    }

    public Question randQuestion() {
        int index = 0;
        int total = 0;
        do {
            if (level < 4) {
                index = Utils.rand(0, 50);
            } else {
                index = Utils.rand(0, CanvasGame.qList.length - 1);
            }
            System.out.println("Index:" + index);
            total++;
            if (total == CanvasGame.qList.length * 2) {
                Question q = getQuestionOutOfPatience();
                if (q != null) {
                    q.setUsed();
                    return q;
                } else {
                    resetQuestionList();
                }
            }
        } while (CanvasGame.qList[index].isUsed());
        System.out.println("Try:" + total);
        System.out.println("Piece:" + index + "-path:" + CanvasGame.qList[index].getPath());
        CanvasGame.qList[index].setUsed();
        return CanvasGame.qList[index];
    }

    public void resetQuestionList() {
        for (int i = 0; i < CanvasGame.qList.length; i++) {
            CanvasGame.qList[i].setUsed(false);
        }
    }

    public Question getQuestionOutOfPatience() {
        for (int i = 0; i < CanvasGame.qList.length; i++) {
            if (!CanvasGame.qList[i].isUsed()) {
                return CanvasGame.qList[i];
            }
        }
        return null;
    }

    public Button[][] getlArray() {
        return lArray;
    }

    public void setlArray(Button[][] lArray) {
        this.lArray = lArray;
    }

    public Button[][] getpArray() {
        return pArray;
    }

    public void setpArray(Button[][] pArray) {
        this.pArray = pArray;
    }

    public Question[][] getqArray() {
        return qArray;
    }

    public void setqArray(Question[][] qArray) {
        this.qArray = qArray;
    }

    public Question getSpecialQuestion() {
        return specialQuestion;
    }

    public void setSpecialQuestion(Question specialQuestion) {
        this.specialQuestion = specialQuestion;
    }
    
    public Question getQuestionByID(int id){
        int i = id/3;
        int j = id%3;
        return qArray[i][j];
    }
}
